//
//  GaoLogger.m
//
//  Created by GikkiAres on 2020/3/28.
//  Copyright © 2020 GikkiAres. All rights reserved.
//

#import "JsLogger.h"

@implementation JsLogger
/**
 1,单例模式的logger,全部使用单例模式的target
 2,新建的logger,用新建的target,需要进行配置.
 */
+ (instancetype)sharedInstance {
    static JsLogger * _logger;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
		_logger = [JsLogger new];
		_logger.logLevel = JsLogLevelVerbose;
		_logger.logTags = nil;
		_logger.consoleTarget = [JsLogTargetConsole sharedInstance];
		_logger.fileTarget = [JsLogTargetDailyRollingFile sharedInstance];
    });
    return _logger;
}

-(instancetype)initWithLogDirPath:(NSString *)logDirPath initialContent:(NSString *)initialContent{
	if(self = [super init]) {
		self.logLevel = JsLogLevelVerbose;
		self.logTags = nil;
		self.consoleTarget = [JsLogTargetConsole new];
		self.fileTarget = [[JsLogTargetDailyRollingFile alloc]initWithLogDirPath:logDirPath maxFileCount:7 initalContent:initialContent];
	}
	return self;
}


-(instancetype)init {
	if(self = [super init]) {
		self.logLevel = JsLogLevelVerbose;
		self.logTags = nil;
		self.consoleTarget = [JsLogTargetConsole new];
		self.fileTarget = [JsLogTargetDailyRollingFile new];
	}
	return self;
}

- (void)log:(JsLogEvent *)logEvent {
	if([self canLogEvent:logEvent]) {
		[self logEvent:logEvent];
	}
}


- (BOOL)canLogEvent:(JsLogEvent *)event {
    //1 过滤器设置的内容模块和此次输出的内容模块有交集
    //2 此次输出的log等级大于等于多虑器中设置的登记.
    if( event.logLevel < self.logLevel) {
        return NO;
    }
	return YES;
}

- (void)logEvent:(JsLogEvent *)logEvent {
			//根据配置的不同logTarget进行输出;
#ifdef DEBUG
			//只有在Debug模式下,才输出到控制台.
		if(logEvent.logTarget&JsLogTargetOptionConsole) {
			[self.consoleTarget log:logEvent];
		}
#endif
		if(logEvent.logTarget&JsLogTargetOptionFile) {
			[self.fileTarget log:logEvent];
		}
}



@end

